import {BASE_URL} from '../settings'
import axios from "axios"
import {Loading, Message} from "element-ui"

axios.defaults.withCredentials = true
export const request = (options) => {
  let loadingInstance = Loading.service({
    lock: true,
    text: '加载中',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  })
  let url = BASE_URL + options.url
  const token = sessionStorage.getItem('token')
  return new Promise((resolve, reject) => {
    axios({
      url,
      method: options.method || 'GET',
      data: options.data || {},
      params: options.data || {},
      headers: {token},
    }).then(res => {
      if (res.data.code != 200) {
        Message.error(res.data.msg)
      } else {
        resolve(res.data)
      }
      loadingInstance.close()
    }).catch(err => {
      Message.error('请求服务器失败')
      reject(err)
      loadingInstance.close()
    })
  })
}
